package com.code.ddbond.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.ddbond.mapper.UserDetailMapper;
import com.code.ddbond.mapper.UserInfoMapper;
import com.code.ddbond.pojo.UserDetail;
import com.code.ddbond.pojo.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail> implements UserDetailService
{
    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void increaseFanAndFollowCount(Long userDetailId, Integer currentUserId)
    {
        // 通过userDetailId查找UserDetail并增加粉丝数
        UserDetail userDetail = userDetailMapper.selectById(userDetailId);
        if (userDetail == null) {
            throw new RuntimeException("User detail not found.");
        }
        userDetail.setFansNums(userDetail.getFansNums() + 1);
        userDetailMapper.updateById(userDetail);

        // 通过当前登录用户的ID查找其对应的UserDetail并增加关注数
        UserDetail currentUserDetail = userDetailMapper.selectOne(
                new QueryWrapper<UserDetail>().eq("fk_user_id", currentUserId));
        if (currentUserDetail == null) {
            throw new RuntimeException("Current user's detail not found.");
        }
        currentUserDetail.setFollowNums(currentUserDetail.getFollowNums() + 1);
        userDetailMapper.updateById(currentUserDetail);
    }

    @Override
    public void increaseContributeNums(Integer userId) {
        UserDetail userDetail = userDetailMapper.selectOne(new QueryWrapper<UserDetail>().eq("fk_user_id", userId));
        if (userDetail != null) {
            userDetail.setContributeNums(userDetail.getContributeNums() + 1);
            userDetailMapper.updateById(userDetail);
        }
    }
}
